<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        #addPopup .layui-form {
            width: 80%;
            text-align: left;
            margin: 10px 10px 10px 10px
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">平台Id</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">昵称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nickName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addBtn"> 新增</button>
                <button class="layui-btn layui-btn-sm layui-btn data-delete-btn" lay-event="import">导入</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete">删除</button>
            </div>
        </script>

        <div id="addPopup" style="display:none">
            <div class="layui-form  layuimini-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">平台ID</label>
                    <div class="layui-input-block">
                        <input type="text" name="userId" value="" id="userId" class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <div id="importPopup" style="display:none">
            <div style="display: flex">
                <div id="download-template" style="padding-left: 20px;padding-top: 20px;">
                    <button type="button" class="layui-btn" onclick="downloadTemplate()">
                        下载模板
                    </button>
                </div>
                <div style="padding-left: 20px;padding-top: 20px;" id="upload-data">
                    <button type="button" class="layui-btn" id="UploadData">
                        <i class="layui-icon">&#xe67c;</i>导入名单
                    </button>
                </div>
            </div>
        </div>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/sys/user/page/invalid/list',
            method: 'GET',
            contentType: 'application/x-www-form-urlencoded',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox"},
                {
                    field: 'numbering', title: '序号', width: 60, align: 'center', templet: function (d) {
                        return d.LAY_INDEX; //返回序号
                    }
                },
                {field: 'userId', title: '平台ID', align: 'center'},
                {field: 'nickName', title: '平台昵称', align: 'center'},
                {field: 'phone', title: '关联手机', align: 'center'},
                {
                    field: 'subscribedWx', title: '绑定公众号（是/否）', sort: true, align: 'center', templet: function (d) {
                        if (d.subscribedWx === '1') {
                            return '<span style="color:#4fc44f;">是</span>'
                        } else {
                            return '<span style="color:#ff0000;">否</span>'
                        }
                    }
                },
                {field: 'createTime', title: '注册时间', sort: true, align: 'center'},
            ]],
            limits: [50, 100, 150, 200, 250, 300],
            limit: 50,
            page: true,
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            response: { //定义后端 json 格式，详细参见官方文档
                statusName: 'code', //状态字段名称
                statusCode: '200', //状态字段成功值
                msgName: 'msg', //消息字段
                countName: 'total', //总数字段
                dataName: 'rows' //数据字段
            },
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'addBtn') {
                // 设置输入框为空
                $("#userId").val('')
                // 新增
                const index = layer.open({
                    title: '新增无效用户信息',
                    type: 1,
                    btn: ['确定', '取消'],
                    content: $('#addPopup'),
                    area: ['30%', '180px'],
                    success: function (layero, index) {

                    },
                    yes: function (index, layero) {
                        // 组装参数
                        const requestData = {
                            userId: $("#userId").val()
                        }
                        $.ajax({
                            type: "post",
                            url: '/sys/user/invalid/add',
                            data: JSON.stringify(requestData),
                            dataType: 'json',
                            contentType: 'application/json',
                            success: function (req) {
                                if (req.code === '200') {
                                    layer.msg("操作成功");
                                    layer.close(index);
                                    reloadTable();
                                } else {
                                    layer.msg(req.msg);
                                    layer.close(index);
                                }
                            },
                            error: function (e) {
                                layer.msg("操作失败，请重试");
                            }
                        })
                    },
                    btn2: function (index, layero) {
                        // 关闭弹窗
                    },
                    end: function () {
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length > 0) {
                    layer.confirm('确定删除吗', function (index) {
                        let idList = []
                        for (const i in data) {
                            idList.push(data[i].userId);
                        }
                        $.ajax({
                            type: "post",
                            url: '/sys/user/invalid/delete',
                            data: JSON.stringify(idList),
                            dataType: 'json',
                            contentType: 'application/json',
                            success: function (req) {
                                if (req.code === '200') {
                                    layer.msg("已删除");
                                    reloadTable();
                                } else {
                                    layer.msg(req.msg);
                                }
                            },
                            error: function (e) {
                                layer.msg("删除失败");
                            }
                        })
                        layer.close(index);
                    });
                } else {
                    layer.alert('请至少选中一行数据');
                }
            }
            if (obj.event === 'import') {  // 监听导入操作
                let myMsg;
                const index = layer.open({
                    type: 1,
                    title: "导入无效客户名单",
                    content: $('#importPopup'),
                    area: ['270px', '140px'],
                    success: function (layero, index) {
                        //文件上传
                        layui.use('upload', function () {
                            let upload = layui.upload;
                            //执行实例
                            let uploadInst = upload.render({
                                elem: '#UploadData' //绑定元素
                                , url: '/sys/user/import/invalid/list' //上传接口
                                , accept: 'file' //允许上传的文件类型
                                , before: function (obj) {
                                    myMsg = layer.msg("文件上传中...", {
                                        icon: 16,
                                        time: -1
                                    })
                                    layer.close(index);
                                    layer.load(); //上传loading
                                }
                                , done: function (res) {
                                    //上传完毕回调
                                    layui.use('layer', function () {
                                        let layer = layui.layer;
                                        layer.close(myMsg);//手动关闭
                                        layer.msg(res.msg);
                                    });
                                    //两秒后重载表格数据
                                    setTimeout(function () {
                                        window.location.reload();
                                    }, 2000);
                                }
                                , error: function () {
                                    //请求异常回调
                                    layui.use('layer', function () {
                                        let layer = layui.layer;
                                        layer.msg("导入失败！");
                                    });
                                }
                            });
                        });
                    }
                });
            }
            return false;
        });

        form.on('checkbox(filterUsers)', function (data) {
            if (data.elem.checked) { //判断复选框是否选中
                filterUsers = true;
            } else {
                filterUsers = false;
            }
            reloadTable();
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            return false;
        });

    });


    /**
     * 重载表格数据
     */
    function reloadTable() {
        layui.use(['table'], function () {
            let table = layui.table;
            table.reload('currentTableId', {
                url: '/sys/user/page/invalid/list',
            });
        })
    }

    function downloadTemplate() {
        downloadExcel("/sys/user/download/import/invalid/template", null, '导入无效用户数据模板.xlsx', 'POST')
    }
</script>

</body>
</html>